﻿using System;
using System.Data;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using log4net;
using Microsoft.ApplicationBlocks.Data;
namespace AnyOffice.general.system.workflow.flow_type
{
    public partial class type_view_graph : ModulePage
    {
        public int flowId;
        private static ILog log= LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public string vml = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.flowId = (Request.QueryString["flow_id"] == null) ? 0 : int.Parse(Request.QueryString["flow_id"].ToString());
                this.ViewState["FlowId"] = this.flowId;
                DataTable table = new FlowTypeDao().SelectByFlowId(base.Conn, this.flowId);
                if ((table.Rows.Count > 0) && (table.Rows[0]["FlowDesc"] != DBNull.Value))
                {
                    this.pnlFlowDesc.Visible = true;
                    this.litFlowDesc.Text = table.Rows[0]["FlowDesc"].ToString();
                }
                this.GetInfo();
            }
            if (this.Page.IsPostBack)
            {
                if ((Request.Form["FLOW_ID"] != null) && (Request.Form["SET_SQL"] != null))
                {
                    this.flowId = Convert.ToInt32(Request.Form["FLOW_ID"].ToString());
                    string str = Request.Form["SET_SQL"].ToString();
                    if (str.EndsWith(";"))
                    {
                        str = str.Substring(0, str.Length - 1);
                    }
                    string[] strArray = str.Split(new char[] { ';' });
                    if (strArray.Length > 0)
                    {
                        for (int i = 0; i < strArray.Length; i++)
                        {
                            try
                            {
                                SqlHelper.ExecuteNonQuery(base.Conn, CommandType.Text, "UPDATE Flow_Process SET " + strArray[i]);
                            }
                            catch (Exception exception)
                            {
                                if (log.IsErrorEnabled)
                                {
                                    log.Error("", exception);
                                }
                            }
                        }
                    }
                }
                this.GetInfo();
            }
        }
        private void GetInfo()
        {
            if (this.flowId != 0)
            {
                DataTable table = new FlowProcessDao().SelectByFlowId(base.Conn, this.flowId);
                if (table.Rows.Count > 0)
                {
                    StringBuilder builder = new StringBuilder();
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        VmlControl control = new VmlControl();
                        control.Index = i;
                        control.Id = Convert.ToInt32(table.Rows[i]["ID"].ToString());
                        control.PrcsId = Convert.ToInt32(table.Rows[i]["PrcsId"].ToString());
                        if (i < (table.Rows.Count - 1))
                        {
                            control.NextPrcsId = Convert.ToInt32(table.Rows[i + 1]["PrcsId"].ToString());
                        }
                        control.PrcsName = table.Rows[i]["PrcsName"].ToString();
                        control.PrcsTo = (table.Rows[i]["PrcsTo"] != DBNull.Value) ? table.Rows[i]["PrcsTo"].ToString() : "";
                        control.SetLeft = Convert.ToInt32(table.Rows[i]["SetLeft"].ToString());
                        control.SetTop = Convert.ToInt32(table.Rows[i]["SetTop"].ToString());
                        control.PrcsCount = table.Rows.Count;
                        builder.Append(control.GetVml());
                    }
                    this.vml = builder.ToString();
                }
            }
        }
    }
}
